<?php
/**
 * Created by lzl
 * Date: 2021 2021/10/14
 * Time: 13:06
 */
namespace App\Services\Api;

use App\Exceptions\RequestException;
use App\Forms\CartForms;
use App\Forms\GoodsAttributeForms;
use App\Forms\GoodsForms;
use App\Models\Good;
use App\Services\BaseServices as Base;

class CartServices extends Base
{

    /**
     * 添加购物车
     * @param $validated
     * @return bool
     * @throws RequestException
     */
    public function addToCart($validated)
    {
//        $goodsForm = new GoodsForms();
//
//        $goods_id = $validated['goods_id'];

//        $goods = $goodsForm->getGoodsDetail($goods_id);

//        if($goods->is_single == Good::SINGLE_MORE){
//
//            if (!isset($validated['sku']) || !count($validated['sku'])) {
//                throw new RequestException('添加购物车商品异常');
//            }
//
//            $attribute_list = (new GoodsAttributeForms())->getGoodsAttributeList($goods_id);
//
//            foreach ($attribute_list as $item) {
//                foreach ($validated['sku'] as $v) {
//
//                    $attribute_name = array_values(json_decode($item['attribute_name'], true));
//
//                    //TODO sku没设计好 导致这里拿字符串比较 有机会重构
//                    if (!array_diff($attribute_name, $v)) {
//
//                        $validated['attribute_id'] = $item->id;
//                        break 2;
//                    }
//                }
//            }
//
//        }

        return (new CartForms())->addOrEditCart($validated, UID);

    }


    /**
     * 删除
     * @param $validated
     * @return bool
     * @throws RequestException
     */
    public function deleteCart($validated)
    {
        return (new CartForms())->deleteCart($validated['cart_ids']);
    }


    /**
     * 购物车列表
     * @param $validated
     * @return mixed
     */
    public function cartList($validated)
    {
        $validated['uid'] = UID;

        return (new CartForms())->cartList($validated);
    }


    /**
     * @return int
     */
    public function cartCount()
    {
        return (new CartForms())->cartCount(['uid' => UID]);
    }
}